package com.aos.eap.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.App;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.BuyoffBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.ParseException;

import butterknife.BindView;
import butterknife.OnClick;

public class MFG_BuyoffCheckActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etLiaoTiao)
    FreeUI_ClearEditText etLiaoTiao;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvBuyoffId)
    TextView tvBuyoffId;
    @BindView(R.id.tvCreateUser)
    TextView tvCreateUser;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private BuyoffBean buyoffBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_buyoff_check;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Buyoff送验");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_BuyoffCheckActivity.this));
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_BuyOffSendLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<BuyoffBean>>(
                new TypeToken<BaseResponseBean<BuyoffBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<BuyoffBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    buyoffBean = response.body().getResponse().getBody();
                    setPageData(buyoffBean);
                } else {
                    buyoffBean = null;
                    setPageData(new BuyoffBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BuyoffBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_BuyOffSendLoad");
                buyoffBean = null;
                setPageData(new BuyoffBean());
            }
        });
    }

    private void setPageData(BuyoffBean buyoffBean) {
        tvLotId.setText(buyoffBean.getLotID());
        tvBuyoffId.setText(buyoffBean.getOrderID());
        tvCreateUser.setText(buyoffBean.getOrderUser());
    }

    @OnClick({R.id.tvBtnOne, R.id.tvBtnTwo})
    public void onViewClicked(View view) {
        if (buyoffBean == null) {
            showToast("未获取到页面信息，请重新扫描设备编号", false);
            etEqpId.setText("");
            MyUtils.setEditTextFocus(etEqpId);
            return;
        }
        switch (view.getId()) {
            case R.id.tvBtnOne:
                String remark = etRemark.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(remark)) {
                    showToast("请在备注栏输入自检不良问题", false);
                    MyUtils.setEditTextFocus(etRemark);
                    return;
                }
                JsonObject map = new JsonObject();
                map.addProperty("EqpID", buyoffBean.getEqpID());
                map.addProperty("LotID", buyoffBean.getLotID());
                map.addProperty("OrderID", buyoffBean.getOrderID());
                map.addProperty("OrderUser", buyoffBean.getOrderUser());
                map.addProperty("StripLoc", etLiaoTiao.getText().toString().trim());
                map.addProperty("Remark", etRemark.getText().toString().trim());
                map.addProperty("SenderUser", Constant.CURRENT_USER);
                try {
                    map.addProperty("SenderTime", FreeApi_DateUtils.longToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                map.addProperty("IsOpNG", "NG");
                FreeUI_GeneralFragmentDialog tipDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(App.app,
                        "确定要提交自检NG吗？", () -> {
                            requestSubmit(map);
                        }, () -> {
                        });
                tipDialog.show(getSupportFragmentManager(), "DIALOG");
                break;
            case R.id.tvBtnTwo:
                String liaoTiao = etLiaoTiao.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(liaoTiao)) {
                    showToast("请输入料条位置", false);
                    MyUtils.setEditTextFocus(etLiaoTiao);
                    return;
                }
                JsonObject map2 = new JsonObject();
                map2.addProperty("EqpID", buyoffBean.getEqpID());
                map2.addProperty("LotID", buyoffBean.getLotID());
                map2.addProperty("OrderID", buyoffBean.getOrderID());
                map2.addProperty("OrderUser", buyoffBean.getOrderUser());
                map2.addProperty("StripLoc", etLiaoTiao.getText().toString().trim());
                map2.addProperty("Remark", etRemark.getText().toString().trim());
                map2.addProperty("SenderUser", Constant.CURRENT_USER);
                try {
                    map2.addProperty("SenderTime", FreeApi_DateUtils.longToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                map2.addProperty("IsOpNG", "OK");
                requestSubmit(map2);
                break;
        }
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_BuyOffSend", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_BuyoffCheckActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_BuyOffSend");
            }
        });
    }
}
